<!DOCTYPE html>
<html lang="cn">

<head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- <script src="/test/es6-import.test/entry.bundle.js"></script> -->
    <script src="/dist/fetch-source.js"></script>
</head>

<body>
    <button id="connect" type="button">connect</button>
    <button id="get" type="button">fetch</button>

    <script>
        const videoId = '123';
        // const server = 'seed-server.api.mgtv.com';
        const server = "http://localhost:8800";
        let fs = null;

        document.getElementById('connect').addEventListener('click', function () {
            fs = new fetchSource(server, videoId, {
                timeout: 2,
                onreport(byteLength, type) {
                    if (type === fetchSource.CDN) {
                        console.log(`来自CDN的${byteLength}流量`);
                    } else if (type === fetchSource.P2P) {
                        console.log(`来自P2P的${byteLength}流量`);
                    }
                }
            });
        });


        document.getElementById('get').addEventListener('click', function () {
            const resourceUrl = 'http://pcvideoyf.titan.mgtv.com/c1/2017/07/14_0/78C8E373956A5552537E8900CC18768C_20170714_1_1_440_mp4/FF4F7356789B1418855CFE57BCC5BE1F_40080_50080_395_v02_mp4.ts?arange=0&pm=aoniBgp2oXY0xrE0shv1nHdzeMY9p1hd_zCUXXAwtTrYHdVBIcp6TUyNq86HuB~ydkXdNSdc7r19scC~TRQCcNAj5E3bXjbs10OEP6mupAxEdTtzlRr~nNxH_qVi0J~JFJw1BIYn35FEfm5aj8x6yYMxbWEVwxChr2Kf7nGCgG6SObDQkv8BAXZfr1m7uGm5uMdu17sIuTN1J6z5UsxsL8ORFmA4vIxO5sAugMWBrZWwcjXuS3k_03c3oSH25wN50cuj09h~hwk3dw3blFzhapOJVRPMzbcCAYrwt3~LkpZKsK46L9UFsLmI5x8R1nWlwwMqcprOQ7fcN1myLYJg40DoPY0hp_aM31fa1bwEwICq_ciAaWovto1o70bI6ttXBvJT4gVDLhKdDmdQ1pNVHQ--';
            const timeout = 1500;
            const forceCDN = false;

            fs.fetch(resourceUrl, {
                timeout,
                forceCDN
            }).then(({ buf, p2pbw, cdnbw, type, support, size, loadtime, starttime, speed }) => {
                console.log(buf); //[ArrayBuffer] 
                console.log(p2pbw, cdnbw, type, support, loadtime, size, speed);
            }).catch(err => {
                console.error(err);
            });
        });
    </script>
</body>

</html>